package com.feige.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author feige
 */
@Data
public class SecurityUser implements UserDetails {

    private static final long serialVersionUID = -2994054985935513896L;
    private String id;

    private String token;

    private String email;

    private String avatar;

    private String username;

    private Integer sex;

    private String nickname;

    private String phoneNumber;

    @JsonIgnore
    private String password;

    @ApiModelProperty(value = "用户角色,0管理员，1普通用户")
    private Integer role;

    private Integer status;

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> strings = new HashSet<>();
        SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.toString());
        strings.add(authority);
        return strings;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return password;
    }

    @JsonIgnore
    @Override
    public String getUsername() {
        return email;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
